package com.daervin.app;

import com.daervin.svc.common.dto.*;
import com.daervin.svc.common.exception.CommonException;
import com.daervin.svc.common.exception.ErrorCode;
import com.daervin.svc.common.utils.ValidateUtil;
import com.daervin.svc.manager.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author daervin
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequestMapping("/api")
public class ApiController {
    @Resource
    private CategoryManager categoryManager;
    @Resource
    private SupplierManager supplierManager;
    @Resource
    private ProductManager productManager;
    @Resource
    private StockManager stockManager;
    @Resource
    private SettingManager settingManager;
    @Resource
    private ImageManger imageManger;

    @RequestMapping("/login")
    public ResponseVO<UserDTO> login(HttpServletRequest request, @RequestBody UserDTO requestUser) {
        UserDTO user = settingManager.matchUser(requestUser);
        request.getSession().setAttribute("user", user);
        return ResponseVO.success(user);
    }

    @RequestMapping("/user")
    public ResponseVO<UserDTO> getUser(HttpServletRequest request) {
        UserDTO user = (UserDTO) request.getSession().getAttribute("user");
        if (user == null) {
            throw new CommonException(ErrorCode.LOGIN_EXPIRE);
        }
        user.setCanConfig("zscao".equalsIgnoreCase(user.getUsername()));
        return ResponseVO.success(user);
    }

    @RequestMapping("/setting/list")
    public ResponseVO<List<CategoryTagValDTO>> getSetting() {
        return ResponseVO.success(settingManager.getAll());
    }

    @PostMapping("/setting/update")
    public ResponseVO<Boolean> addSetting(@RequestBody AddSettingDTO request) {
        return ResponseVO.success(settingManager.addOrUpdate(request));
    }

    @RequestMapping("/category/tags")
    public ResponseVO<List<CategoryTagMixDTO>> getCategoryTags() {
        return ResponseVO.success(categoryManager.getCategoryTags());
    }

    @RequestMapping("/category/list")
    public ResponseVO<List<WhCategoryDTO>> getCategoryList() {
        return ResponseVO.success(categoryManager.getCategory());
    }

    @PostMapping("/category/add")
    public ResponseVO<Boolean> addCategory(@RequestBody WhCategoryDTO request) {
        ValidateUtil.checkAddCategory(request);
        return ResponseVO.success(categoryManager.addOrUpdate(request));
    }

    @RequestMapping("/supplier/list")
    public ResponseVO<List<WhSupplierDTO>> getSupplierList() {
        return ResponseVO.success(supplierManager.getSupplier());
    }

    @PostMapping("/supplier/add")
    public ResponseVO<Boolean> addSupplier(@RequestBody WhSupplierDTO request) {
        return ResponseVO.success(supplierManager.addOrUpdate(request));
    }

    @PostMapping("/product/add")
    public ResponseVO<Boolean> addProduct(@RequestBody WhProductDTO request) {
        ValidateUtil.checkWhProduct(request);
        return ResponseVO.success(productManager.addOrUpdate(request));
    }

    @PostMapping("/product/list")
    public ResponseVO<PageResultDTO<WhProductDTO>> getProductList(@RequestBody SearchProductDTO request) {
        ValidateUtil.checkSearchProduct(request);
        return ResponseVO.success(productManager.getByPage(request));
    }

    @RequestMapping("/product/detail")
    public ResponseVO<WhProductDTO> getProductDetail(@RequestParam(name = "productId") Long productId) {
        ValidateUtil.checkProductId(productId);
        return ResponseVO.success(productManager.getDetail(productId));
    }

    @PostMapping("/product/updateStock")
    public ResponseVO<Boolean> updateProductStock(@RequestBody UpdateProductStockDTO request) {
        ValidateUtil.checkUpdateProductStock(request);
        return ResponseVO.success(productManager.updateProductStock(request));
    }

    @PostMapping("/stock/record")
    public ResponseVO<PageResultDTO<WhStockRecordDTO>> getProductRecord(@RequestBody SearchStockRecordDTO request) {
        return ResponseVO.success(stockManager.getRecordByPage(request));
    }

    @RequestMapping("/stock/record/remove")
    public ResponseVO<WhProductDTO> removeProductRecord(@RequestParam(name = "recordId") Long recordId) {
        ValidateUtil.checkProductId(recordId);
        return ResponseVO.success(stockManager.removeRecord(recordId));
    }

    @PostMapping("/upload")
    public ResponseVO<String> uploadImage(@RequestParam("file") MultipartFile image) {
        return ResponseVO.success(imageManger.upload(image));
    }

    @PostMapping("/scanbar/upload")
    public ResponseVO<List<ScanBarResultDTO>> uploadScanBar(@RequestBody ScanBarRequestDTO request) {
        return productManager.uploadScanBar(request);
    }
}
